#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
    string filename;
    char choose;
    while (1) {
        cout << "Please enter the choose (c - character | w - word | s - sentence | d - code line | e - empty lines | m - comment lines | q - quit): ";
        cin >> choose;
        if (choose == 'q') {
            break; // 退出程序；
        }
        cout << "Please enter the filename: ";
        cin >> filename;
        ifstream file(filename);
        if (!file) {
            cout << "Error: counld't to open the file." << endl<<endl;
            continue; // 文件错误，请重新输入；
        }
        string ch;
        int charCount = 0;
        int wordCount = 0;
        int sentCount = 0;
        int cCount = 0;//代码行计数
        int eCount = 0;//空行计数
        int coCount = 0;//注释行计数
        bool CommentBlock = false;//判断注释行
        bool CodeLine = true;//判断是否为代码行
        while (getline(file, ch)) {
            if (ch.empty()) {
                eCount++;
            }
            else {
                for (size_t i = 0; i < ch.length(); i++) {
                    char c = ch[i];
                    charCount++;
                    if (c == ' ' || c == '\t') {
                            wordCount++;
                        CodeLine = false;
                    }
                    else if (c == '.' || c == '?' || c == '!') {
                        sentCount++;
                        if (CodeLine) {
                            wordCount++;
                        }
                        CodeLine = false;
                    }
                    else if (c == '/' && i + 1 < ch.length()) {
                        if (ch[i + 1] == '/' || (ch[i + 1] == '*' && !CommentBlock)) {
                            coCount++;
                            CodeLine = false;
                            break;
                        }
                    	else if (ch[i + 1] == '*') {
                        	CommentBlock = true;
                        	coCount++;
                        	CodeLine = false;
                        	i++; // 跳过'*'；
                        	}
                    }
                    else if (c == '*' && i + 1 < ch.length() && ch[i + 1] == '/') {
                        CommentBlock = false;
                        cCount++;
                        CodeLine = false;
                        i++; // 跳过'/'；
                    }
                }
                if (CodeLine) {
                    cCount++;
                }
            }
        }
        file.close();
        switch (choose) {
        case 'c':
            cout << "Character count: " << charCount << endl<<endl;
            break;
        case 'w':
            cout << "Word count: " << wordCount << endl<<endl;
            break;
        case 's':
            cout << "Sentence count: " << sentCount << endl<< endl;
            break;
        case 'd':
            cout << "Code line count: " << cCount << endl<< endl;
            break;
        case 'e':
            cout << "Empty line count: " << eCount << endl<< endl;
            break;
        case 'm':
            cout << "Comment line count: " << coCount << endl<<endl;
            break;
        default:
            cout << "sry! Please enter again." << endl<<endl;
            break;
        }
    }
    return 0;
}